<?php

class UserModel
{
    static public function getAllUsers()
    {
        $db = DbGateway::getConnection();

        $sql = '
            select
                u.*,
                ul.name as user_level_name
            from
                user as u
                join user_level as ul on (ul.id = u.user_level)
            order by
                u.id desc
        ';

        $users = $db->fetchAll($sql);
        if (!empty($users))
        {
            return $users;
        }

        return null;
    }

    static public function getUserData($user_id)
    {
        $db = DbGateway::getConnection();

        $sql = 'select * from user where id = ' . intval($user_id);

        $user_data = $db->fetchSingle($sql);
        if (!empty($user_data))
        {
            return $user_data;
        }

        return null;
    }

    static public function getUserLevels()
    {
        $db = DbGateway::getConnection();

        $sql = '
            select
                *
            from
               user_level
            order by
                id desc
        ';

        $levels = $db->fetchAll($sql);
        if (!empty($levels))
        {
            return $levels;
        }

        return null;
    }

    static public function insertUser($user_data)
    {
        $db = DbGateway::getConnection();

        $sql = '
            insert into user
                (email, password, first_name, last_name, user_level, friendly_name, on_line)
            values
                (:email, :password, :first_name, :last_name, :user_level, :friendly_name, :on_line)
        ';

        $user_data['friendly_name'] = self::_generateFriendlyName($user_data['first_name'] . ' ' . $user_data['last_name']);

        $db->bindParam(':email', $user_data['email']);
        $db->bindParam(':password', md5($user_data['password']));
        $db->bindParam(':first_name', $user_data['first_name']);
        $db->bindParam(':last_name', $user_data['last_name']);
        $db->bindParam(':user_level', $user_data['user_level']);
        $db->bindParam(':friendly_name', $user_data['friendly_name']);
        $db->bindParam(':on_line', $user_data['on_line']);

        $result = $db->affect($sql);
        return ($result > 0) ? true : false;
    }

    static public function updateUser($user_data, $with_password = false)
    {
        $db = DbGateway::getConnection();

        $sql = '
            update user set
                email = :email,
                ' . (($with_password) ? 'password = :password,' : '') . '
                first_name = :first_name,
                last_name = :last_name,
                user_level = :user_level,
                friendly_name = :friendly_name,
                on_line = :on_line
            where
                id = :id
        ';

        $user_data['friendly_name'] = self::_generateFriendlyName($user_data['first_name'] . ' ' . $user_data['last_name']);

        $db->bindParam(':email', $user_data['email']);
        if ($with_password)
        {
            $db->bindParam(':password', md5($user_data['password']));
        }
        $db->bindParam(':first_name', $user_data['first_name']);
        $db->bindParam(':last_name', $user_data['last_name']);
        $db->bindParam(':user_level', $user_data['user_level']);
        $db->bindParam(':friendly_name', $user_data['friendly_name']);
        $db->bindParam(':on_line', $user_data['on_line']);
        $db->bindParam(':id', $user_data['id']);

        $result = $db->affect($sql);
        return ($result > 0) ? true : false;
    }

    static protected function _generateFriendlyName($string)
    {
        $string = preg_replace(
            array('/Č/u', '/Ć/u', '/Ž/u', '/Đ/u', '/Š/u', '/č/u', '/ć/u', '/ž/u', '/đ/u', '/š/u'),
            array('C', 'C', 'Z', 'D', 'S', 'c', 'c', 'z', 'd', 'u'),
            $string
        );
        $string = strtolower($string);
        $string = preg_replace('/[^a-z\s]/', '', $string);
        $string = trim($string);
        $string = preg_replace('/\s+/', '-', $string);

        return $string;
    }
}

?>